package server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ResourceBundle;
import java.util.regex.Pattern;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;

public class GetUrl extends Thread {
	DBObject obj;
	public GetUrl(DBObject obj) {
		this.obj = obj;
		this.start();
	}

	public void run(){
		try {
			URL url = new URL(obj.get("sitemap").toString());
			
			String charset   = "UTF-8";	
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();		    
		    connection.setInstanceFollowRedirects(true);
		    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Keep-Alive", "115");
			connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
			connection.setRequestProperty("Cache-Control", "max-age=0");
		    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName(charset)));
			String content = "";
			char[] buf = new char[102102];
			int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
			Document doc = Jsoup.parse(content);
			
			String link = "";
			String type = obj.get("type").toString();
			String reglink = obj.get("reglink").toString();
			
			try {
				if(type.equals("html"))
				{
					String[] arrreglink = reglink.split("\\|");
					if(arrreglink.length>=2)
						link = doc.select(arrreglink[0]).get(Integer.parseInt(arrreglink[1])).attr("href");
					else
						link = doc.select(reglink).get(0).attr("href");
				}
				else
				{
					link = doc.select(reglink).get(0).text().replaceAll("#comments", "");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

			if(!Pattern.matches("^http://[\\w\\W]+$",link))
			{
				link = "http://"+Utils.fomatDmains(obj.get("sitemap").toString())+link;
			}
			
			if(!link.equals(""))
			{
				if(obj.get("link")==null || (obj.get("link")!=null && !obj.get("link").toString().equals(link)))
				{
					obj.put("link", link);
					
					DBCollection coll = Data.data("urlCraw");
					coll.save(obj);
					System.out.println(link);
					setUp();
					createProducerAndSendAMessage(link+"|"+obj.get("username"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static ActiveMQConnectionFactory connectionFactory;
    private static Connection connection =null;
    private static Session session;
    private static Destination destination;
    private static boolean transacted = false;
	private static void setUp() throws JMSException, IOException {
		ResourceBundle resource = ResourceBundle.getBundle("config");		
         connectionFactory = new ActiveMQConnectionFactory(
                 ActiveMQConnection.DEFAULT_USER,
                 ActiveMQConnection.DEFAULT_PASSWORD,
                 "failover:tcp://"+resource.getString("server")+":61616");
         if(connection == null)
         {
	         connection = connectionFactory.createConnection();
	         connection.start();
         }
         session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
         
         destination = session.createQueue(resource.getString("queue_get_content"));
     }
 
     private static void createProducerAndSendAMessage(String str) throws JMSException {
   	  
         MessageProducer producer = session.createProducer(destination);
         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
         TextMessage message = session.createTextMessage(str);
         System.out.println("Sending message: " + message.getText());
         producer.send(message);
//         connection.close();
     }
}
